package com.gjzt.business.media.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.servlet.ReqBean;
import com.gjzt.business.media.service.MediaTypeService;

@Controller
@RequestMapping(value = "/mediaType")
public class MediaTypeController {

    @Autowired
    private MediaTypeService mediaTypeService;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryMediaType(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(mediaTypeService.findMediaType(reqBean));
    }

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/queryTopType")
    @ResponseBody
    public JSONObject queryTopType(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(mediaTypeService.queryTopType(reqBean));
    }

    /**
     * 保存
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public JSONObject saveMediaType(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	if(reqBean.containsKey("mediaId")){
    		mediaTypeService.updateMediaType(reqBean);
    	}else{
    		mediaTypeService.addMediaType(reqBean);
    	}
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateMediaType(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        mediaTypeService.updateMediaType(reqBean);
        return Response.responseSuccess();
    }
    
    /**
     * 失效
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/{mediaId}/disable")
    @ResponseBody
    public JSONObject disableMediaType(@PathVariable String mediaId) {
    	mediaTypeService.disableMediaType(mediaId);
        return Response.responseSuccess();
    }
    
    /**
     * 生效
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/{mediaId}/revert")
    @ResponseBody
    public JSONObject revertMediaType(@PathVariable String mediaId) {
    	mediaTypeService.revertMediaType(mediaId);
        return Response.responseSuccess();
    }
    
   /**
    * 根据父类型查询下面的子类型
    * @param reqBean
    * @return
    */
    @RequestMapping(value = "/getMediaTypesByParentId")
    @ResponseBody
    public JSONObject getMediaTypesByParentId(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(mediaTypeService.getMediaTypesByParentId(reqBean));
    }

    @RequestMapping(value = "/all")
    @ResponseBody
    public JSONObject getAllMediaTypes(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(mediaTypeService.getAllMediaTypes(reqBean));
    }
    
    
    @RequestMapping(value = "/{mediaId}/items")
    @ResponseBody
    public JSONObject getItemsOfMedia(@PathVariable String mediaId) {
        return Response.responseSuccess(mediaTypeService.getItemsOfMedia(mediaId));
    }
}